#import "SEGCustomWholeView.h"

static CGFloat customContentViewTopPadding = 14;//内容和视图之间的间距

@implementation SEGCustomWholeViewModel
-(instancetype)init{
    if (self == [super init]) {
        _time = @"";
        _year = @"";
        _month = @"";
        _day = @"";
        _describe = @"";
        
        _objId = @"";
        _content =@"";
        _objType = @"";
        _operatorType = @"";
        _pgcType = @"";
        
        _shareId = @"";
        _shareTitle = @"";
        _widgetWidth = LYScreenWidth();
    }
    return self;
}
@end

/**************************SEGCustomWholeViewLayout************************/
@implementation SEGCustomWholeViewLayout
- (instancetype)initWithData:(SEGCustomWholeViewModel *)data type:(SEGEnumNeighborViewType)type{
    if (self == [super init]) {
        _data = data;
        _type = type;
    }
    [self SEGCustomWholeViewLayout];
    return self;
}

-(void)SEGCustomWholeViewLayout{
    //高度固定
//    _totalHeight = kNeighborViewHeight;
    
    //高度不固定
    //_totalHeight = 0;
    _dayLabelHeight= 0;
    _dayLabelTextLayout = nil;
    _monthLabelHeight = 0;
    _monthLabelTextLayout = nil;
    _describeLabelHeight = 0;
    _describeLabelTextLayout = nil;
    if (!_data) return;
    
    [self layout];
    
    _totalHeight = 0;
    if (_data.describe) {
        _totalHeight += 15;
        _totalHeight += _describeLabelHeight;
    }
    
    if (_data.cusDataSource) {
        _totalHeight += customContentViewTopPadding;
        _totalHeight += _cusLayout.totalHeight;
    }
    
    _totalHeight += 0.5;
}

//数据处理
-(void)layout{
    if (_data.isToday == YES) {
        [self segdayLabelWithContent:@"今天" fontSize:20 textColor:kColor_1];
    }else{
        [self segdayLabelWithContent:_data.day fontSize:20 textColor:kColor_1];
        [self segmonthLabelWithContent:_data.month fontSize:13 textColor:kColor_1];
    }
    [self segdescribeLabelWithContent:_data.describe fontSize:13 textColor:kColor_3];
    [self segCustomLayout];
}

-(void)segCustomLayout{
    _cusLayout = [[SEGcustomContentViewLayout alloc]initWithData:_data.cusDataSource type:_data.cusType];
}

//控件dayLabel文本布局计算
-(void)segdayLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont systemFontOfSize:fontSize];//[UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    text.yy_strokeWidth = @(-4);
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    modifier.lineHeightMultiple = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(LYScreenWidth(), HUGE);
    container.linePositionModifier = modifier;
    
    _dayLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_dayLabelTextLayout)return;
    _dayLabelHeight = [modifier heightForLineCount:_dayLabelTextLayout.rowCount];
}

//控件monthLabel文本布局计算
-(void)segmonthLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont systemFontOfSize:fontSize];//[UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    modifier.lineHeightMultiple = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(LYScreenWidth(), HUGE);
    container.linePositionModifier = modifier;
    
    _monthLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_monthLabelTextLayout)return;
    _monthLabelHeight = [modifier heightForLineCount:_monthLabelTextLayout.rowCount];
}

//控件describeLabel文本布局计算
-(void)segdescribeLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont systemFontOfSize:fontSize];//[UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    modifier.lineHeightMultiple = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(LYScreenWidth(), HUGE);
    container.linePositionModifier = modifier;
    
    _describeLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_describeLabelTextLayout)return;
    _describeLabelHeight = [modifier heightForLineCount:_describeLabelTextLayout.rowCount];
}

@end

/**************************SEGCustomWholeView************************/
@implementation SEGCustomWholeView
- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = [UIScreen mainScreen].bounds.size.width;
        frame.size.height = kNeighborViewHeight;
    }
    self = [super initWithFrame:frame];
    
    [self addSubview:self.dayLabel];
    [self addSubview:self.monthLabel];
    [self addSubview:self.describeLabel];
    
    [self addSubview:self.customContentView];
    
    [self addSubview:self.lineView];
    self.lineView.hidden=YES;
    
//    self.backgroundColor = kColor_T;
    return self;
}

-(void)setWithLayout:(SEGCustomWholeViewLayout *)layout{
    self.height = layout.totalHeight;
    self.width = layout.data.widgetWidth;
    
    _layout = layout;
    
    if (self.height <= 0) return;
    
    self.dayLabel.textLayout = layout.dayLabelTextLayout;
    self.monthLabel.textLayout = layout.monthLabelTextLayout;
    self.describeLabel.textLayout = layout.describeLabelTextLayout;
    
    [self.customContentView setWithLayout:layout.cusLayout];
    
    [self setupUI];
}

//界面布局
-(void)setupUI{
    self.describeLabel.hidden = NO;
    self.customContentView.hidden = NO;
   // self.lineView.hidden = NO;
    
    if (_layout.data.shouldHiddenDate) {
        self.dayLabel.hidden = YES;
        self.monthLabel.hidden = YES;
    }else {
        self.dayLabel.hidden = NO;
        if (_layout.data.isToday) {//今天不显示月份
            self.monthLabel.hidden = YES;
        }else {
            self.monthLabel.hidden = NO;
        }
    }

    //时间 - 天
    [self.dayLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.top.equalTo(self.describeLabel).offset(-2);
        make.height.mas_equalTo(_layout.dayLabelHeight);
    }];
    
    //时间 - 月
    [self.monthLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dayLabel.mas_left);
        make.top.equalTo(self.dayLabel.mas_bottom).offset(1);
        make.height.mas_equalTo(_layout.monthLabelHeight);
    }];
    
    //副标题描述
    [self.describeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(15);
        make.left.equalTo(self).offset(70);
        make.height.mas_equalTo(_layout.describeLabelHeight);
    }];

    [self.customContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.describeLabel.mas_bottom).offset(customContentViewTopPadding);
        make.left.equalTo(self.mas_left).offset(70);
        make.right.equalTo(self.mas_right).offset(-15);
        make.height.mas_equalTo(self.customContentView.layout.totalHeight);
    }];
    
    //线条
    [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.customContentView.mas_bottom);
        make.left.equalTo(self).offset(70);
        make.right.equalTo(self.mas_right);
        make.height.mas_equalTo(0.5);
    }];
    
    
    self.monthLabel.textAlignment = NSTextAlignmentLeft;
    self.monthLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
    
    self.dayLabel.textAlignment = NSTextAlignmentLeft;
    self.dayLabel.textVerticalAlignment = SEGTextVerticalAlignmentTop;
    
    self.describeLabel.textAlignment = NSTextAlignmentLeft;
    self.describeLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
    
}


#pragma mark - getter
-(SEGLabel *)monthLabel{
    if (!_monthLabel) {
        _monthLabel = [[SEGLabel alloc]init];
        _monthLabel.userInteractionEnabled = NO;
        _monthLabel.textAlignment = NSTextAlignmentCenter;
        _monthLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _monthLabel.displaysAsynchronously = YES;
        _monthLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _monthLabel.fadeOnHighlight = NO;
        _monthLabel.numberOfLines = 0;
        _monthLabel.fadeOnAsynchronouslyDisplay = NO;
        _monthLabel.fadeOnAsynchronouslyDisplay = NO;
        _monthLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        // if (kDebug)_monthLabel.backgroundColor = kSegRandomColor;
    }
    return _monthLabel;
}

-(SEGLabel *)dayLabel{
    if (!_dayLabel) {
        _dayLabel = [[SEGLabel alloc]init];
        _dayLabel.userInteractionEnabled = NO;
        _dayLabel.textAlignment = NSTextAlignmentCenter;
        _dayLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _dayLabel.displaysAsynchronously = YES;
        _dayLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _dayLabel.fadeOnHighlight = NO;
        _dayLabel.numberOfLines = 0;
        _dayLabel.fadeOnAsynchronouslyDisplay = NO;
        _dayLabel.fadeOnAsynchronouslyDisplay = NO;
        _dayLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        // if (kDebug)_dayLabel.backgroundColor = kSegRandomColor;
    }
    return _dayLabel;
}

-(SEGLabel *)describeLabel{
    if (!_describeLabel) {
        _describeLabel = [[SEGLabel alloc]init];
        _describeLabel.userInteractionEnabled = NO;
        _describeLabel.textAlignment = NSTextAlignmentCenter;
        _describeLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _describeLabel.displaysAsynchronously = YES;
        _describeLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _describeLabel.fadeOnHighlight = NO;
        _describeLabel.numberOfLines = 0;
        _describeLabel.fadeOnAsynchronouslyDisplay = NO;
        _describeLabel.fadeOnAsynchronouslyDisplay = NO;
        _describeLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        // if (kDebug)_describeLabel.backgroundColor = kSegRandomColor;
    }
    return _describeLabel;
}

-(UIView *)lineView{
    if (!_lineView){
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = kColor_L2;
    }
    return _lineView;
}

-(SEGcustomContentView *)customContentView{
    if (!_customContentView){
        _customContentView = [[SEGcustomContentView alloc]init];
        // if (kDebug)_customContentView.backgroundColor = kSegRandomColor;
    }
    return _customContentView;
}

@end

